/****
 * VIP 服务公共处理Api接口Controller
 * 
 * @author Haijun Gao 
 * @date 2017-4-28 下午4:10:27
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */
package com.smforj.ssm.vip.restful.web.controller;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smforj.ssm.cache.TokenStorage;
import com.smforj.ssm.frame.core.enums.RouterMapping;
import com.smforj.ssm.frame.core.web.controller.BaseControllerSupportImpl;
import com.smforj.ssm.frame.core.web.domain.DataResult;
import com.smforj.ssm.frame.core.web.domain.DataResult.DataStatus;
import com.smforj.ssm.sys.web.bean.SysUser;
import com.smforj.ssm.sys.web.bean.query.MemberQuery;
import com.smforj.ssm.sys.web.bean.vo.MemberVo;
import com.smforj.ssm.sys.web.bean.vo.SysUserVo;
import com.smforj.ssm.sys.web.service.MemberService;
import com.smforj.ssm.sys.web.service.SysUserService;
import com.smforj.ssm.vip.restful.web.bean.TestVo;


@RouterMapping("")
@Controller
@RequestMapping("/api/vip")  
public class ApiController extends BaseControllerSupportImpl {
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private MemberService memberService;  
	@Autowired
	private SysUserService userService;
	 
	/***
	 * 登陆
	 * @param username 用户名
	 * @param password 密码
	 * @param clientInfo 客户端信息
	 * @return
	 * @date 2017-5-30 下午1:55:07
	 */
	@RequestMapping(value="/login",method = RequestMethod.POST,
			produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult<SysUserVo> login(@RequestParam("username") String username,@RequestParam("password") String password,
			@RequestParam("clientInfo") Serializable clientInfo) {  
		
		DataResult<SysUserVo> result = new DataResult<SysUserVo>();
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
		{
			result.setStatus(DataStatus.ERROR);
			result.setMessage("username or password require not null");
		}else
		{
			SysUser u = userService.getUser(username, password);
			if(u != null)
			{
				SysUserVo user = (SysUserVo) u;  
				long t = System.currentTimeMillis();
				String cipherText = TokenStorage.getInstance().getToken(
						user.getUserName() + "|" + t);
				// System.out.println("密文:" + cipherText);
				user.set("access_token", cipherText); 
				user.set("expires_in", 3600);
				user.set("display_name", user.getNickname()); 

				result.setStatus(DataStatus.OK);
				result.setMessage("success login");
				result.setData(user); 
				// 更新一下最后登陆时间。
				TokenStorage.getInstance().updateLoginTime(cipherText, username, t);  
			}else
			{
				result.setStatus(DataStatus.ERROR);
				result.setMessage("username or password is illegal");
			}
		}
		return result;
	} 

	/***
	 * 注销
	 * @param token access_token
	 * @return
	 * @date 2017-5-30 下午1:55:41
	 */
	@RequestMapping(value="/layout",produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult<String> logout(@RequestParam("access_token") String token) { 
		DataResult<String> result = new DataResult<String>();
		if (StringUtils.isEmpty(token)) {
			result.setStatus(DataStatus.ERROR); 
			result.setMessage( "用于认证的token为空。"); 
		} else {
			// 更新一下最后登陆时间。
			int rv = TokenStorage.getInstance().removeToken(token);
			if (rv == 0) { 
				result.setStatus(DataStatus.OK); 
				result.setMessage( "用户登出成功。");  
			} else {
				result.setStatus(DataStatus.ERROR); 
				result.setMessage( "用于认证的token非法。");  
			}
		}
		return result;
	}
	/****
	 * 
	 * @param rfid
	 * @param token 目前未使用
	 * @return
	 * @date 2017-5-30 下午1:15:57
	 */
	@RequestMapping(value={"/member/{rfid}"},method = RequestMethod.GET,
			produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult<MemberVo> showMember(@PathVariable("rfid") String rfid,@RequestParam("access_token") String token)
	{     
		log.info("showMember");
		DataResult<MemberVo> result = new DataResult<MemberVo>();
		/*if(checkToken(token))
		{ */
			MemberQuery query = new MemberQuery();
			query.setRfid(rfid);
			MemberVo member = memberService.queryOne(query); 
			result.setData(member);
			result.setStatus(DataStatus.OK);
			result.setMessage("success");
/*		}else
		{
			result.setStatus(DataStatus.ERROR);
			result.setMessage("token is illegal");
		} */
		return result;
	}  
	
	@RequestMapping(value={"/test"},method = RequestMethod.POST,
			consumes = MediaType.APPLICATION_JSON_VALUE,
			produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult<MemberVo> addTest(@RequestBody MemberVo model,@RequestParam String access_token)
	{     
		log.info("addTest");
		DataResult<MemberVo> result = new DataResult<MemberVo>(); 
		result.addOK("", model);
		return result;
	}
	
	/****
	 * 远程添加会员信息
	 * @param model 会员对象
	 * @param access_token access_token
	 * @return 保存的会员对象或错误信息
	 * @date 2017-7-21 上午11:06:05
	 */
	@RequestMapping(value={"/member"},method = RequestMethod.POST,
			consumes = MediaType.APPLICATION_JSON_VALUE,
			produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public DataResult<MemberVo> addMember(@RequestBody MemberVo model,@RequestParam String access_token)
	{     
		log.info("addMember");
		DataResult<MemberVo> result = new DataResult<MemberVo>();  
	/*	if(checkToken(token))
		{   */
			try{
				memberService.insert(model);
				result.addOK("success", null); 
			}catch(Exception ex)
			{
				result.addError("save error"+ex.getMessage()); 
			}

/*		}else
		{
			result.setStatus(DataStatus.ERROR);
			result.setMessage("token is illegal");
		} */
		return result;
	} 
	
	/***
	 * 检测Token是否合法
	 * @param token
	 * @return
	 * @date 2017-5-30 下午3:03:14
	 */
	private Boolean checkToken(String token)
	{
		return TokenStorage.getInstance().checkToken(token) == 0 ? true : false; 
	}
	
}
